Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Calendar.Components

Namespace C4F.DevKit.Calendar.Controls
	''' <summary>
	''' This control display a list of appointments from a calendar.
	''' </summary>
	<ToolboxItem(True), ToolboxBitmap(GetType(AppointmentsList))> _
	Public Partial Class AppointmentsList
		Inherits UserControl
		#Region "Private Data"

		Private _appts As List(Of Appointment) = Nothing

		Private _calendarService As CalendarService

		#End Region

		#Region "Constants"

		Private Const UID_GRID_COLUMN_NAME As String = "colHdrUID"

		Private Const UID_FIELD As String = "UID"
		Private Const LOCAL_START_DATETIME_FIELD As String = "LocalStartDateTime"
		Private Const SUMMARY_FIELD As String = "Summary"
		Private Const LOCATION_FIELD As String = "Location"
		Private Const NOTES_FIELD As String = "Notes"

		#End Region

		#Region "Properties"

		''' <summary>
		''' The appointments viewed in this control.
		''' </summary>
		<Browsable(False)> _
		Public Property Appointments() As List(Of Appointment)
			Get
				Return _appts
			End Get
			Set(ByVal value As List(Of Appointment))
				_appts = value

				If Not _appts Is Nothing Then
					_appts.Sort(AddressOf CompareAppointments)
				End If

				ConfigureBindings()
			End Set
		End Property

		''' <summary>
		''' The UID of the currently selected appointment.
		''' </summary>
		<Browsable(False)> _
		Public ReadOnly Property SelectedAppointmentID() As Guid
			Get
				Dim returnValue As Guid = Guid.Empty

				If dataGridView1.SelectedRows.Count > 0 AndAlso Not dataGridView1.SelectedRows(0).Cells(UID_GRID_COLUMN_NAME).Value Is Nothing Then
					Dim appt As Appointment = GetAppointmentFromList(CType(dataGridView1.SelectedRows(0).Cells(UID_GRID_COLUMN_NAME).Value, Guid))

					If Not appt Is Nothing Then
						returnValue = appt.UID
					End If
				End If

				Return returnValue
			End Get
		End Property

		''' <summary>
		''' The currently selected appointment.
		''' </summary>
		<Browsable(False)> _
		Public ReadOnly Property SelectedAppointment() As Appointment
			Get
				If dataGridView1.SelectedRows.Count > 0 Then
					Return Me.GetAppointmentFromList(CType(dataGridView1.SelectedRows(0).Cells(UID_GRID_COLUMN_NAME).Value, Guid))
				Else
					Return Nothing
				End If
			End Get
		End Property

		''' <summary>
		''' The CalendarService to use with this control and with its
		''' appointments.
		''' </summary>
		<Browsable(True)> _
		Public Property CalendarService() As CalendarService
			Get
				Return _calendarService
			End Get
			Set(ByVal value As CalendarService)
				_calendarService = value
			End Set
		End Property

		#End Region ' Properties

		#Region "Constructor"

		''' <summary>
		''' Creates a new AppointmentList control.
		''' </summary>
		Public Sub New()
			InitializeComponent()

			If (Not DesignMode) Then
				ConfigureBindings()
			End If
		End Sub

		#End Region ' Constructor

		#Region "Private Methods"

		Private Sub ConfigureBindings()
			bindingSource1.DataSource = _appts

			colHdrDate.DataPropertyName = LOCAL_START_DATETIME_FIELD
			colHdrSubject.DataPropertyName = SUMMARY_FIELD
			colHdrLocation.DataPropertyName = LOCATION_FIELD
			colHdrNotes.DataPropertyName = NOTES_FIELD
			colHdrUID.DataPropertyName = UID_FIELD
		End Sub

		Private Function GetAppointmentFromList(ByVal uid As Guid) As Appointment

			Dim returnValue As Appointment = Nothing
			For Each appt As Appointment In _appts
				If appt.UID = uid Then
					returnValue = appt
					Exit For
				End If
			Next appt

			Return returnValue
		End Function

		Private Function CompareAppointments(ByVal leftAppt As Appointment, ByVal rightAppt As Appointment) As Integer
			If leftAppt Is Nothing Then
				If rightAppt Is Nothing Then
					Return 0
				Else
					Return -1
				End If
			ElseIf rightAppt Is Nothing Then
				Return 1
			Else
				Dim leftStart As DateTime = leftAppt.StartDateTime
				Dim rightStart As DateTime = rightAppt.StartDateTime

				If leftStart = rightStart Then
					Return 0
				ElseIf leftStart < rightStart Then
					Return -1
				Else
					Return 1
				End If
			End If
		End Function

		#End Region ' Private Methods

		#Region "Public Methods"

		''' <summary>
		''' Refreshes the list of appointments.
		''' </summary>
		''' <remarks>The CalendarService property must not be empty.</remarks>
		Public Sub RefreshList()
			If CalendarService Is Nothing Then
				Throw New CalendarServiceIsNullException("The calendar service property has not been set.")
			End If

			Appointments = TryCast(CalendarService.GetAllAppointments(), List(Of Appointment))
		End Sub

		#End Region ' Public Methods
	End Class
End Namespace